<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<view class="left">
				<u-icon name="arrow-left" size="32" color="#333" @click="goBack"></u-icon>
			</view>
			<view class="title">注册账号</view>
			<view class="right"></view>
		</view>
		
		<!-- 注册表单 -->
		<view class="register-form">
			<view class="input-item">
				<u-icon name="phone" size="40" color="#666"></u-icon>
				<input type="number" v-model="form.phone" placeholder="请输入手机号" maxlength="11"/>
			</view>
			
			<view class="input-item code">
				<u-icon name="chat" size="40" color="#666"></u-icon>
				<input type="number" v-model="form.code" placeholder="请输入验证码" maxlength="6"/>
				<text class="send-code" 
					:class="{'disabled': counting}" 
					@click="sendCode">
					{{counting ? `${countdown}s后重试` : '获取验证码'}}
				</text>
			</view>
			
			<view class="input-item">
				<u-icon name="lock" size="40" color="#666"></u-icon>
				<input type="password" v-model="form.password" placeholder="请设置密码"/>
			</view>
			
			<view class="input-item">
				<u-icon name="lock" size="40" color="#666"></u-icon>
				<input type="password" v-model="form.confirmPassword" placeholder="请确认密码"/>
			</view>
			
			<!-- 用户协议 -->
			<view class="agreement">
				<checkbox :checked="agreed" @click="agreed = !agreed"></checkbox>
				<text class="text">注册即代表同意</text>
				<text class="link" @click="showAgreement('user')">《用户协议》</text>
				<text class="text">和</text>
				<text class="link" @click="showAgreement('privacy')">《隐私政策》</text>
			</view>
			
			<view class="register-btn" :class="{'disabled': !canRegister}" @click="handleRegister">
				<text>注 册</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { register, sendSmsCode } from '@/common/api'
	
	export default {
		data() {
			return {
				form: {
					phone: '',
					code: '',
					password: '',
					confirmPassword: ''
				},
				agreed: false,
				counting: false,
				countdown: 60
			}
		},
		computed: {
			canRegister() {
				return this.form.phone && 
					this.form.code && 
					this.form.password && 
					this.form.confirmPassword && 
					this.agreed
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			validatePhone() {
				const phoneReg = /^1[3-9]\d{9}$/
				if (!phoneReg.test(this.form.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return false
				}
				return true
			},
			async sendCode() {
				if (this.counting) return
				
				if (!this.validatePhone()) return
				
				this.counting = true
				this.countdown = 60
				
				try {
					// 调用真实的发送验证码接口
					const res = await sendSmsCode({
						phone: this.form.phone,
						type: 'register'
					})
					
					if (res.code === 200) {
						uni.showToast({
							title: '验证码已发送',
							icon: 'success'
						})
						
						const timer = setInterval(() => {
							if (this.countdown > 0) {
								this.countdown--
							} else {
								this.counting = false
								clearInterval(timer)
							}
						}, 1000)
					} else {
						this.counting = false
						uni.showToast({
							title: res.msg || '验证码发送失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('发送验证码失败', error)
					this.counting = false
					uni.showToast({
						title: '验证码发送失败，请稍后重试',
						icon: 'none'
					})
				}
			},
			async handleRegister() {
				if (!this.canRegister) return
				
				if (!this.validatePhone()) return
				
				if (this.form.password !== this.form.confirmPassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none'
					})
					return
				}
				
				uni.showLoading({
					title: '注册中...'
				})
				
				try {
					// 调用真实的注册接口
					const res = await register({
						phone: this.form.phone,
						code: this.form.code,
						password: this.form.password
					})
					
					uni.hideLoading()
					
					if (res.code === 200) {
						uni.showToast({
							title: '注册成功',
							icon: 'success'
						})
						
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					} else {
						uni.showToast({
							title: res.msg || '注册失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('注册失败', error)
					uni.hideLoading()
					uni.showToast({
						title: '注册失败，请稍后重试',
						icon: 'none'
					})
				}
			},
			showAgreement(type) {
				uni.navigateTo({
					url: `/pages/common/agreement?type=${type}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #fff;
		
		.nav-bar {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 30rpx;
			background-color: #fff;
			
			.title {
				font-size: 36rpx;
				font-weight: bold;
				color: #333;
			}
		}
		
		.register-form {
			padding: 60rpx 40rpx;
			
			.input-item {
				display: flex;
				align-items: center;
				height: 100rpx;
				background-color: #F7F7FA;
				border-radius: 50rpx;
				padding: 0 40rpx;
				margin-bottom: 30rpx;
				
				input {
					flex: 1;
					height: 100%;
					font-size: 28rpx;
					color: #333;
					margin-left: 20rpx;
				}
				
				&.code {
					.send-code {
						font-size: 28rpx;
						color: #1890FF;
						padding-left: 30rpx;
						
						&.disabled {
							color: #999;
						}
					}
				}
			}
			
			.agreement {
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 40rpx 0;
				
				checkbox {
					transform: scale(0.7);
					margin-right: 8rpx;
				}
				
				.text {
					font-size: 24rpx;
					color: #999;
				}
				
				.link {
					font-size: 24rpx;
					color: #1890FF;
				}
			}
			
			.register-btn {
				height: 88rpx;
				background-color: #1890FF;
				border-radius: 44rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				
				text {
					font-size: 32rpx;
					color: #fff;
					font-weight: bold;
				}
				
				&.disabled {
					background-color: #ccc;
					opacity: 0.6;
				}
			}
		}
	}

	.forget-link {
		color: #3c9cff;
		font-size: 28rpx;
		margin-top: 20rpx;
		text-align: center;
	}

	.login-link {
		text-align: center;
		margin-top: 40rpx;
		font-size: 28rpx;
		
		text {
			color: #3c9cff;
		}
	}
</style>